package com.itheima.service.mongo.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;
//交给ioc容器
@Component
public class RedisPidService {
    //注入stringredis
    @Autowired
    private StringRedisTemplate redisTemplate;
    //生成动态自增的·ID
    public Long getNextPid(String collecationName) {
        // 创建long类型主键自增器对象
        RedisAtomicLong redisAtomicLong = new RedisAtomicLong("pid:" + collecationName, redisTemplate.getConnectionFactory());
        // 自增并返回
        return redisAtomicLong.incrementAndGet();
    }
}
